from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from common.doLog import DoLog


class BasePage():
    # 登录及用户名元素
    login_uname = (By.XPATH, "//*[@id='ECS_MEMBERZONE']/a[1]")
    # 注册以及退出元素
    register_loginout = (By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[2]')

    # 实例并且调用
    def __init__(self, d=None):
        if d is None:
            self.wd = webdriver.WebDriver()
            self.wd.get("http://192.168.110.134/")
        else:
            self.wd:webdriver = d

        self.wd.maximize_window()
        self.wd.implicitly_wait(10)


    # 显式等待的
    def getElement(self, locater):
        logger = DoLog().set_get_logger()
        try:
            e: WebElement = WebDriverWait(self.wd, 2, 1).until(lambda x: x.find_element(*locater))
        except Exception as e1:
            logger.errer(f"发生了异常，查找元素失败{locater}，错误元素{e1}")
        else:
            logger.info(f"查找元素{locater}成功：")
            return e
        finally:
            logger.info("查找元素结束")
    # 获取元素的文本
    def getElementValue(self, locater):
        return self.getElement(locater).text
    # 获取alert的文本
    def getAlertValue(self):
        alert = self.wd.switch_to.alert
        t = alert.text
        alert.accept()
        return t
